export default {
	async getAddressList() {
		let params = {
				pageSize:10,
				pageNum:1
		}
		let res = await this.httptool.post(this.apis.addressList,params);
		this.addressList = res.data.list;
		if(this.isNotAddress()){
			this.addressList.unshift(this.$store.state.confrimorder.address)
		}else{
			
		}
		
		console.log(this.$store.state.confrimorder.address)
//		this.changeAddress();
	},
	isNotAddress(){		
		for(let item of this.addressList){			
			if(item.id == this.$store.state.confrimorder.address.id){
				return false;
			}
		}
		return true;
	},
	async getAddressDetail(id, successBack, errorBack) {
		let params = {
			id: id
		};
		this.httptool.ajaxPost(this.apis.getAddressDetail, params, function(res) {
			this.params = res.data;
			if(this.addressInfo&&this.addressInfo.latitude){
				this.params.latitude = this.addressInfo.latitude;
				this.params.longitude = this.addressInfo.longitude;
				this.params.addressPre = this.addressInfo.address;
			}
		});
	},
	orderPreview() {		
		this.httptool.ajaxPost(this.apis.previewOrder, this.params, res=>{			
			this.tableData = res.data.list;
			this.totalMoney = res.data.priceCount;
			console.log(this.tableData)
		});
	},
	orderAdd(){
		let params = {};
		params.address = this.params.address;
		params.ids = this.params.ids;
		params.subjectId = this.params.subjectId;
		params.traffickerId = this.params.traffickerId;
		let leavemsgs = [];
		for(let item of this.tableData) {
			let leavemsg = {};
			leavemsg.leavemsg = item.leavemsg;
			for(let car of item.list) {
				leavemsg.id = car.shopCarId;
				leavemsgs.push(leavemsg);
			}
		}
		params.leavemsg = JSON.stringify(leavemsgs);
		this.httptool.ajaxPost(this.apis.orderAdd, params, res=> {
			if(res.code == 1) {
//				this.$router.replace("orders?type=wait_paid");
				let orderids={};
				orderids.ids=res.data;
				this.httptool.ajaxPost('sign/handSign', orderids, res=>{
					console.log(res);
					if(res.data){
//						this.$router.push(res.data.signUrl);
						window.location.href=res.data.signUrl;
					}
				});
			} else {
				this.$toast('提交失败！');
			}
		});
	},
	async delAddress(id) {
		let params = {
			id: id
		};
//		this.$confirm('确定要删除吗？', '提示', {
//			confirmButtonText: '确定',
//			cancelButtonText: '取消',
//			type: 'warning'
//		}).then(() => {
//			this.httptool.post(this.apis.delAddress, param, res=> {
//				
//				this.$message.success('删除成功！');
//				this.getAddressList();
//				
//			});
//		}).catch(() => {
//
//		});
		let res=await this.httptool.post(this.apis.delAddress,params);
		this.getAddressList();
	},
	async setDefaultAddr( item, status) {
		let defParams = {};
		defParams.id = item.id;
		defParams.pageNum = 1
		defParams.pageSize = 10
		for(let item_ of this.addressList){
			if(item_.id!=item.id){
				item_.defaultAddress = false;
			}else{
				item_.defaultAddress = true;
			}
		}
		this.selectAddress(item);
//		let res=await this.httptool.post(this.apis.setDefaultAddress,defParams);
//		this.addressList = res.data.list;	
//		this.changeAddress();

	},
	async handleSave() {
		let that = this;
		let rule = [{
			name: 'name',
			label: '收货人',
			rule: ['validateEmpty_']
		}, {
			name: 'tel',
			label: '手机号',
			rule: ['validateTel_']
		}, {
			name: 'addressPre',
			label: '地址',
			rule: ['validateEmptyCity_']
		}, {
			name: 'addressDetail',
			label: '详细地址',
			rule: ['validateEmpty_']
		}]

		let rulesAddress = [{
			name: 'address',
			label: '地址',
			rule: ['validateEmpty_']
		}];
		if(!this.validatetool.validate(this, this.params, rule)) {
			return;
		}
		let msg = '添加成功!';
		let requestUrl = this.apis.addAddress; //添加
		if(this.addrId) {
			msg = '修改成功!';
			requestUrl = this.apis.editAddress; //修改
		};
		let res=await this.httptool.post(requestUrl,this.params);
		if(this.type == 'order') {
				this.$router.replace('/orderconfirm');
		} else {
//				history.back();
			this.reload();
		}
		
	},
	async getDefaultAddress(id, successBack, errorBack) { //id>0,取对应的地址，id=0,取默认地址
		
		let res = await this.httptool.post(this.apis.getDefaultAddress,null);		
		this.params.address  = res.data;
		this.orderPreview();
//		if(id) {
//			let params = {
//				id: id
//			};
//			this.httptool.ajaxPost(this.apis.getAddressDetail, params, function(res) {
//				successBack(res.data);
//			});
//		} else {
//			this.httptool.ajaxPost(this.apis.getDefaultAddress, {}, function(res) {
//				successBack(res.data);
//			});
//		}
	}
}